﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using SoGame.Helper;
using SoGame.Model;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using System.Windows.Controls.Primitives;
using SoGame.Views;

namespace SoGame.ViewModels
{
    public class NewPlayerViewModel : BaseViewModel
    {
        private bool _CanDisplayKinectSource;
        private bool _CanDisplayNormalSource;
        private SoGameDbContext context = new SoGameDbContext();

        public override dynamic Model
        {
            get
            {
                return this.currentPlayer;
            }
        }

        private bool _isEditMode;
        public bool IsEditMode
        {
            get { return _isEditMode; }
            set
            {
                _isEditMode = value;
                this.OnPropertyChanged(() => this.IsEditMode);
            }
        }

        public ICommand SaveCommand { get { return new BaseCommand(Save); } }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        private void Save()
        {
            if (!string.IsNullOrEmpty(this.Error))
            {
                return;
            }
            //if (_PlayerImage != null)
            //{
            //    currentPlayer.Photo = ImageHelper.ConvertImageBMPToByteArray(_PlayerImage);
            //}
            if (CurrentPlayer.ID == 0)
            {
                context.SavePlayer(CurrentPlayer);
            }
            else
            {
                context.UpdatePlayer(CurrentPlayer);
            }

            OnPropertyChanged(() => Players);
            _PlayerImage = null;
            this.CurrentPlayer = new Player();
        }

        public ICommand CancelCommand { get { return new BaseCommand(Cancel); } }
        private void Cancel()
        {
            _PlayerImage = null;
            CurrentPlayer = new Player();
        }

        private GamePlay _currentGamePlay;
        public GamePlay CurrentGamePlay
        {
            get { return _currentGamePlay;}
            set
            {
                _currentGamePlay = value;
                this.OnPropertyChanged(()=> this.CurrentGamePlay);
            }
        }

        public ICommand ShowPhoto { get { return new BaseCommand(Photo); } }

        private void Photo()
        {
            if (CurrentGamePlay != null)
                (new PhotoPopup() { DataContext = CurrentGamePlay}).ShowDialog();
        }

        private void StartKinect()
        {
            if (KinectHelper.Instance.IsKinectStarted)
            {
                KinectHelper.Instance.StartRecognition();
                _CanDisplayKinectSource = true;
                _CanDisplayNormalSource = !_CanDisplayKinectSource;

                OnPropertyChanged(() => CanDisplayKinectSource);
                OnPropertyChanged(() => CanDisplayNormalSource);

                KinectHelper.Instance.PropertyChanged += (ks, ke) =>
                {
                    if (ke.PropertyName == "ImageSource")
                    {
                        OnPropertyChanged(() => KinectSource);
                    }
                };
                KinectHelper.Instance.PropertyChanged += (ks, ke) =>
                {
                    if (ke.PropertyName == "CurrentPlayer")
                    {
                        OnPropertyChanged(() => CurrentPlayer);
                        this.OnPropertyChanged(() => Name);
                        this.OnPropertyChanged(() => FirstName);
                        this.OnPropertyChanged(() => Email);
                        this.OnPropertyChanged(() => AuthorizePhotoUsage);
                    }
                };
            }
        }

        private void StopKinect()
        {
            if (KinectHelper.Instance.IsKinectStarted)
            {
                KinectHelper.Instance.PauseKinect();
            }

            _CanDisplayKinectSource = false;
            _CanDisplayNormalSource = !_CanDisplayKinectSource;

            OnPropertyChanged(() => CanDisplayKinectSource);
            OnPropertyChanged(() => CanDisplayNormalSource);

            KinectHelper.Instance.PropertyChanged -= (ks, ke) =>
            {
                if (ke.PropertyName == "ImageSource")
                {
                    OnPropertyChanged(() => KinectSource);
                }
            };
            KinectHelper.Instance.PropertyChanged -= (ks, ke) =>
            {
                if (ke.PropertyName == "CurrentPlayer")
                {
                    OnPropertyChanged(() => CurrentPlayer);
                }
            };
        }

        public ICommand CaptureCommand { get { return new BaseCommand(Capture, () => this.UseKinect); } }
        private void Capture()
        {
            StartKinect();

        }


        public ICommand TakePictureCommand { get { return new BaseCommand(TakePicture); } }
        private void TakePicture()
        {
            if (KinectHelper.Instance.IsKinectStarted)
            {
                //var imageStream = KinectHelper.Instance.TakePicture();
                ////CurrentPlayer.Photo = ConvertersHelper.StreamToByteArray(imageStream);
                //_PlayerImage = ConvertersHelper.StreamToByteArray(imageStream);

                _PlayerImage = KinectHelper.Instance.TakePictureFace();
                OnPropertyChanged(() => PlayerImage);
                currentPlayer.Photo = ImageHelper.ConvertImageBMPToByteArray(_PlayerImage);
                OnPropertyChanged(() => CurrentPlayer);
                StopKinect();

                if (currentPlayer != null && currentPlayer.Photo != null)
                {
                    _CanDisplayNormalSource = true;
                    _CanDisplayKinectSource = !_CanDisplayNormalSource;
                }
            }
        }

        public string FirstName
        {
            get
            {
                return CurrentPlayer.FirstName;
            }
            set
            {
                if (CurrentPlayer.FirstName != value)
                {
                    CurrentPlayer.FirstName = value;
                    this.OnPropertyChanged(() => FirstName);
                }
            }
        }

        public string Name
        {
            get
            {
                return CurrentPlayer.Name;
            }
            set
            {
                if (CurrentPlayer.Name != value)
                {
                    CurrentPlayer.Name = value;
                    this.OnPropertyChanged(() => Name);
                }
            }
        }

        public string Email
        {
            get
            {
                return CurrentPlayer.Email;
            }
            set
            {
                if (CurrentPlayer.Email != value)
                {
                    CurrentPlayer.Email = value;
                    this.OnPropertyChanged(() => Email);
                }
            }
        }

        private BitmapSource _PlayerImage;
        public BitmapSource PlayerImage
        {
            get
            {
                return _PlayerImage;
            }
            set
            {
                if (_PlayerImage != value)
                {
                    _PlayerImage = value;
                    this.OnPropertyChanged(() => PlayerImage);
                }
            }
        }

        /// <summary>
        /// Gets the kinect source.
        /// </summary>
        public System.Windows.Media.Imaging.BitmapSource KinectSource
        {
            get
            {
                return this.UseKinect ? KinectHelper.Instance.ImageSource : null;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [authorize photo usage].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [authorize photo usage]; otherwise, <c>false</c>.
        /// </value>
        public bool AuthorizePhotoUsage
        {
            get { return CurrentPlayer.AuthorizePhotoUsage; }
            set
            {
                if (CurrentPlayer.AuthorizePhotoUsage != value)
                {
                    CurrentPlayer.AuthorizePhotoUsage = value;
                    this.OnPropertyChanged(() => AuthorizePhotoUsage);
                }
            }
        }

        public System.Windows.Visibility CanDisplayKinectSource
        {
            get
            {
                return _CanDisplayKinectSource ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            }
        }

        public System.Windows.Visibility CanDisplayNormalSource
        {
            get { return _CanDisplayNormalSource ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed; }
        }

        private Player currentPlayer;
        public Player CurrentPlayer
        {
            get
            {
                return currentPlayer;
            }
            set
            {
                currentPlayer = value;
                _CanDisplayKinectSource = false;
                _CanDisplayNormalSource = !_CanDisplayKinectSource;

                OnPropertyChanged(() => this.CurrentPlayer);
                this.OnPropertyChanged(() => Name);
                this.OnPropertyChanged(() => FirstName);
                this.OnPropertyChanged(() => Email);
                if (currentPlayer != null && currentPlayer.Photo != null) 
                { 
                    PlayerImage = ImageHelper.LoadImage(currentPlayer.Photo);
                    _CanDisplayNormalSource = true;
                    _CanDisplayKinectSource = !_CanDisplayNormalSource;
                }
                this.OnPropertyChanged(() => CanDisplayKinectSource);
                this.OnPropertyChanged(() => CanDisplayNormalSource);
                this.OnPropertyChanged(() => PlayerImage);
                this.OnPropertyChanged(() => AuthorizePhotoUsage);

                if (currentPlayer != null && currentPlayer.ID != 0)
                    IsEditMode = true;
                else
                    IsEditMode = false;
            }
        }

        public ObservableCollection<Player> Players
        {
            get
            {
                ObservableCollection<Player> players = new ObservableCollection<Player>();
                foreach (var item in context.GetAllPlayers())
                {
                    players.Add(item);
                }
                return players;
            }
        }

        public NewPlayerViewModel()
        {
            CurrentPlayer = new Player();
        }

        public void KinectUp()
        {
            KinectHelper.Instance.KinectCameraUp();
        }

        public void KinectDown()
        {
            KinectHelper.Instance.KinectCameraDown();
        }
    }
}
